/* @(#)FseDocumentSectionHistoryView.java
** 
** Copyright (C) 2012 by Steven D. Stamps
**
**             Trademarks & Copyrights
** Flywheel Management Science(TM), Flywheel Management Model(TM),
** Flywheel Story Editor(TM) and FlywheelMS(TM) are exclusive trademarks
** of Steven D. Stamps and may only be used freely for the purpose of
** identifying the unforked version of this software.  Subsequent forks
** may not use these trademarks.  All other rights are reserved.
**
** DecKanGL (Decorated Kanban Glyph Language) and TribKn (Tribal Knowledge)
** are also exclusive trademarks of Steven D. Stamps.  These may be used
** freely within the unforked FlywheelMS application and documentation.
** All other rights are reserved.
**
** gConGUI (game Controller Graphical User Interface) is an exclusive
** trademark of Steven D. Stamps.  This trademark may be used freely
** within the unforked FlywheelMS application and documentation.
** All other rights are reserved.
**
** Trademark information is available at
** <http://www.flywheelms.com/trademarks>
**
** Flywheel Management Science(TM) is a copyrighted body of management
** metaphors, governance processes, and leadership techniques that is
** owned by Steven D. Stamps.  These copyrighted materials may be freely
** used, without alteration, by the community (users and developers)
** surrounding this GPL3-licensed software.  Additional copyright
** information is available at <http://www.flywheelms.org/copyrights>
**
**              GPL3 Software License
** This program is free software: you can use it, redistribute it and/or
** modify it under the terms of the GNU General Public License, version 3,
** as published by the Free Software Foundation. This program is distributed
** in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
** even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
** PURPOSE.  See the GNU General Public License for more details. You should
** have received a copy of the GNU General Public License, in a file named
** COPYING, along with this program.  If you cannot find your copy, see
** <http://www.gnu.org/licenses/gpl-3.0.html>.
*/

package com.flywheelms.library.fse.views;

import java.util.Date;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.flywheelms.library.FmsLibraryApplication;
import com.flywheelms.library.R;
import com.flywheelms.library.fse.FseDocumentSectionType;
import com.flywheelms.library.fse.model.FseDocument;
import com.flywheelms.library.fse.model.FseDocumentSection;
import com.flywheelms.library.fse.model.FseDocumentSectionHistory;
import com.flywheelms.library.fse.model.FseDocumentSectionParagraphAudit;
import com.flywheelms.library.fse.model.FseDocumentTransactionType;
import com.flywheelms.library.gcg.helper.GcgHelper;

public class FseDocumentSectionHistoryView extends FseDocumentSectionView {
	
	protected LinearLayout documentSectionLayout;
	protected FseDocumentSectionHistory documentSectionHistory = new FseDocumentSectionHistory();
	protected FseTransactionTableView transactionTableView;

	public FseDocumentSectionHistoryView(Context context, AttributeSet attrs) {
		super(context, attrs);
		setSectionType(FseDocumentSectionType.HISTORY);
	}
	
	@Override
	public void initialize(FseDocumentView aDocumentView) {
		super.initialize(aDocumentView);
		setDocumentSectionLayout();
		sizeLayoutToDisplay();  // HACK ALERT - SDS
		initTabControlButtons();
	}

	private void sizeLayoutToDisplay() {
		FrameLayout theFrameLayout = (FrameLayout) this.documentSectionLayout.findViewById(R.id.fse__transaction_scrollview__frame);
		LinearLayout.LayoutParams params =
				(LinearLayout.LayoutParams) theFrameLayout.getLayoutParams();
		int theTabRowHeight = GcgHelper.getPixelsForDp(FmsLibraryApplication.getContext(), 100);
//		int theTargetSize = GcgHelper.getPixelsForDp(FmsLibraryApplication.getContext(), 394);
		int theOtherViewHeights = GcgHelper.getPixelsForDp(FmsLibraryApplication.getContext(), 59);
		int theLayoutHeight = GcgHelper.getScreenHeight() - (theTabRowHeight + theOtherViewHeights) + 3;
		params.height = theLayoutHeight;
		theFrameLayout.setLayoutParams(params);
	}
	
	@Override
	public String getHelpContextUrlString() {
		return getResources().getString(R.string.fse__document_section_history__help_url);
	}

	@Override
	public ViewGroup getDocumentSectionLayout() {
		return this.documentSectionLayout;
	}

	@Override
	public void setDocumentSectionLayout() {
		this.documentSectionLayout =
				(LinearLayout) this.documentView.findViewById(getDocumentSectionViewResourceId());
		this.transactionTableView =
				(FseTransactionTableView) this.documentSectionLayout.findViewById(R.id.fse__transaction_table__view);
	}

	@Override
	void viewDocumentSection(FseDocumentSection aDocumentSection) {
		this.documentSectionHistory = (FseDocumentSectionHistory) aDocumentSection;
		this.transactionTableView.viewDocumentSection(this.documentSectionHistory);
	}

	@Override
	public void activateDocumentSectionTab() {
		super.activateDocumentSectionTab();
		FrameLayout theParentFrameLayout = (FrameLayout) this.documentSectionLayout.getParent();
		theParentFrameLayout.bringChildToFront(this.documentSectionLayout);
		theParentFrameLayout.invalidate();
		this.tabControlHistoryButton.requestFocus();
	}

	@Override
	String sectionIsModified() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void clickNextTabControlButton() {
		deactivateDocumentSectionTab();
		this.tabControlStoryButton.performClick();
	}

	@Override
	public void clickPreviousTabControlButton() {
		deactivateDocumentSectionTab();
		this.tabControlAuditButton.performClick();
	}

	public FseDocumentSectionHistory getDocumentSectionClone() {
		return this.documentSectionHistory.getClone();
	}

	public void updateHistory(
			Date aTimestamp,
			FseDocumentTransactionType aDocumentTransactionType,
			FseDocumentSectionParagraphAudit aStoryAudit,
			FseDocumentSectionParagraphAudit aNotesAudit,
			FseDocument aBaselineFseDocument ) {
		this.documentSectionHistory.updateHistory(aTimestamp, aDocumentTransactionType, aStoryAudit, aNotesAudit, aBaselineFseDocument);
	}

	public void refresh() {
		viewDocumentSection(this.documentSectionHistory);
	}

}
